
* Prepare data for gains from marriage 
* Measurement error in Unofficial Singles, shock  s=10(10)300


forval s=10(10)300 {


use "$data\M_final_dataset.dta" , clear

rename y_survey year
gen hnative= hbc==0
tab hnative
gen wnative= wbc==0
tab wnative 
gen HOM = hbc==wbc
lab var HOM "Homogamous"
gen HET = HOM!=1
lab var HET "Heterogamous"
gen HET_IT =  (hbc==0 & wbc>0) | ( hbc>0 & wbc==0) 
lab var HET_IT "Heterogamous-Italian"
gen HETW = HET==1 & wbc!=0
gen HETH = HET==1 & hbc!=0

gen famt= .
replace famt=1 if wnative==1 & hnative==1
replace famt=2 if wnative==1 & hnative==0
replace famt=3 if wnative==0 & hnative==1
replace famt=4 if wnative==0 & hnative==0
label define famtl 1 "Wife Native-Husband Native" 2 "Wife Native-Husband Foreign" 3 "Wife Foreign-Husband Native" 4 "Wife Foreign-Husband Foreign"
label values famt famtl


* Generate number of marriages

gen nm=1
collapse (sum) nm , by( region provres year wbc hbc HOM HET_IT HET  famt)


* Merge with Single data accounting for Measurement error in Unofficial Singles

merge m:1 provres hbc year using "$data\Msingle_FINAL_shock`s'.dta" , keep(match master) keepusing(Msingle)
tab _merge
drop _merge 
replace Msingle=round(Msingle,1)

merge m:1 provres wbc year using "$data\Fsingle_FINAL_shock`s'.dta" , keep(match master) keepusing(Fsingle)
tab _merge
drop _merge 
replace Fsingle=round(Fsingle,1)


* Merge with population

merge m:1 provres using "$data\population_byprovince.dta" , keep(match master) 
tab _merge
drop _merge

* Generate gains to marriage CS2006

collapse (sum) nm  (mean) Fsingle Msingle , by( region provres year wbc hbc HOM  HET HET_IT famt population)
gen phixy = ln((nm^2)/(Fsingle*Msingle))
tabstat phixy , by(famt) stat( mean sd N)

lab var phixy "gains to marriage"
lab var population "total resident population in the year/province"

save "$data\Gain_marriage_robust_shock`s'.dta" , replace



******************* NATIVE HUSBAND - FOREIGN WIFE **************************

use "$data\Gain_marriage_robust_shock`s'.dta" , clear
keep if hbc==0

* Treatment
gen nat= wbc
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUII)
tab _merge
drop _merge
replace EUII=4 if nat==224 
replace EUII=4 if nat==258  
replace EUII=4 if nat==259  
replace EUII=4 if nat==260  
replace EUII=4 if nat==261  
replace EUII=6 if nat==317  
replace EUII=0 if nat==0  
lab define eui3 0 "ITA" 1 "EU15"  2 "EU2004" 3 "EU2007" 4 "EU_Other" 5 "Africa" 6 "Asia" 7 "America_South" 8 "OECD" 
label values EUII eui3
tab EUII, gen(euii)
rename euii1 ita
rename euii2 eu15
rename euii3 eu2004
rename euii4 eu2007
rename euii5 euother
rename euii6 africa
rename euii7 asia
rename euii8 americas
rename euii9 oecd
codebook EUII
drop if EUII==.

* Time
gen post2004= year>=2004
gen post2007= year>=2007

drop if phixy==.
xtset provres 
order provres year
sort provres year
xtsum phixy 

egen newEUxPostEU=rmax(eu2004post2004 eu2007post2007)
gen wbprov = wbc*1000+provres
gen yearprov = year*1000+provres

tab wbc, gen(dwbc)
forval i=1/159 {
gen wbcyear`i' = dwbc`i'*year
}

tab provres, gen(dprov)
forval i=1/`r(r)' {
gen provtrend`i' = dprov`i'*year
}


********************************************************************************
*  Figure A1: Panel a. Native husband – foreign wife
*  Changes in gains from marriage between natives and new EU citizens – Robustness accounting for Measurement error in Unofficial Singles

areg phixy newEUxPostEU i.year provtrend1-provtrend99 wbcyear1-wbcyear159 [aw=population], absorb(wbprov) vce(cluster provres)
outreg2 using "$output\baseline_shock`s'.xls" , keep(newEUxPostEU) replace 



******************* NATIVE WIFE - FOREIGN HUSBAND **************************

use "$data\Gain_marriage_robust_shock`s'.dta" , clear
keep if wbc==0

* Treatment
gen nat = hbc
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUII)
tab _merge
drop _merge
replace EUII=4 if nat==224 
replace EUII=4 if nat==258  
replace EUII=4 if nat==259  
replace EUII=4 if nat==260  
replace EUII=4 if nat==261  
replace EUII=6 if nat==317  
replace EUII=0 if nat==0 
lab define eui3 0 "ITA" 1 "EU15"  2 "EU2004" 3 "EU2007" 4 "EU_Other" 5 "Africa" 6 "Asia" 7 "America_South" 8 "OECD" 
label values EUII eui3
tab EUII, gen(euii)
rename euii1 ita
rename euii2 eu15
rename euii3 eu2004
rename euii4 eu2007
rename euii5 euother
rename euii6 africa
rename euii7 asia
rename euii8 americas
rename euii9 oecd
codebook EUII
drop if EUII==.

* Time 
gen post2004= year>=2004
gen post2007= year>=2007

drop if phixy==.
xtset provres 
order provres year
sort provres year
xtsum phixy 

egen newEUxPostEU=rmax(eu2004post2004 eu2007post2007)
gen hbprov = hbc*1000+provres
gen yearprov = year*1000+provres

tab hbc, gen(dhbc)
forval i=1/153 {
gen hbcyear`i' = dhbc`i'*year
}

tab provres, gen(dprov)
forval i=1/`r(r)' {
gen provtrend`i' = dprov`i'*year
}

********************************************************************************
*  Figure A1: Panel b. Native wife – foreign husband
*  Changes in gains from marriage between natives and new EU citizens – Robustness accounting for Measurement error in Unofficial Singles

areg phixy newEUxPostEU i.year provtrend1-provtrend99 hbcyear1-hbcyear153 [aw=population], absorb(hbprov) vce(cluster provres)
outreg2 using "$output\baseline_shock`s'.xls" , keep(newEUxPostEU) replace 

}


